﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZBJF.Warranty.Common.Common
{
    public class LogHelper
    {
        public static string directPath = AppDomain.CurrentDomain.BaseDirectory + "logs";

        #region 写错误日志 + static void WriteError(string message)
        /// <summary>
        /// 写错误日志
        /// </summary>
        /// <param name="message">内容</param>
        /// <param name="logType">日志类型</param>
        public static void WriteLog(string message,LogType logType)
        {
            StreamWriter streamWriter = null;
            try
            {
                if (!Directory.Exists(directPath))   //判断文件夹是否存在，如果不存在则创建  
                {
                    Directory.CreateDirectory(directPath);
                }

                string filePath = directPath + string.Format(@"\{0}.log", DateTime.Now.ToString("yyyy-MM-dd") + "_" + Enum.GetName(typeof(LogType), logType));
                //判断文件是否存在如果不存在则创建，如果存在则添加。  
                streamWriter = !File.Exists(filePath) ? File.CreateText(filePath) : File.AppendText(filePath);
                streamWriter.WriteLine("***********************************************************************");
                streamWriter.WriteLine(DateTime.Now.ToString("HH:mm:ss"));
                if (message != null)
                {
                    streamWriter.WriteLine("信息：\r\n" + message);
                }
            }
            catch { }
            finally
            {
                if (streamWriter != null)
                {
                    streamWriter.Flush();
                    streamWriter.Dispose();
                    streamWriter = null;
                }
            }
        } 
        #endregion

    }

    public enum LogType {
        /// <summary>
        /// 错误
        /// </summary>
        error=1,
        /// <summary>
        /// 标记
        /// </summary>
        tag = 2,
        /// <summary>
        /// 记录
        /// </summary>
        remark= 3
    }
}
